
import { getToken, setLogout } from './auth.js'
import { convertObjToURI } from './functions.js'

// 公共接口URL
// export const REQUEST_URL = "http://172.18.20.174:48080"
// 测试环境
export const REQUEST_URL = "https://safe.htkqy.com"
// 正式环境
// export const REQUEST_URL = "https://safe.jinanenergy.cn/"

const Request = {
  get: (url, data = {}, loadingState = false) => {
    if(loadingState) uni.showLoading({ title: '请稍等...' })
    return new Promise((resolve, reject) => {
      uni.request({
        url: REQUEST_URL + url, method: "GET", data,
        header: { 'Authorization': `Bearer ${getToken()}`, 'tenant-id':  '1', 'content-type': 'application/x-www-form-urlencoded' },
        success: res => {
          if (res.data.code === 0) {
            return resolve(res.data.data)
          } else if (res.data.code === 401) {
            setLogout()
            uni.navigateTo({ url: '/pages/login?routeBack=1' })
          } else {
            if(res.data.code == null && res.data.data.code == 0) return resolve(res.data.data)
            return uni.showToast({ title: res.data.msg, icon: 'none' })
          }
        },
        fail: err => {
          return reject(err)
        },
        complete: () => {
          if(loadingState) uni.hideLoading()
        }
      })
    })
  },
	post: (url, data = {}, loadingState = false) => {
    if(loadingState) uni.showLoading({ title: '请稍等...' })
		return new Promise((resolve, reject) =>
			uni.request({
				url: REQUEST_URL + url, method: "POST", data,
        header: { 'Authorization': `Bearer ${getToken()}`, 'tenant-id':  '1', 'content-type': 'application/json; charset=UTF-8' },
        success: res => {
          if (res.data.code === 0) {
            return resolve(res.data.data)
          } else if (res.data.code === 401) {
            setLogout()
            uni.navigateTo({ url: '/pages/login?routeBack=1' })
          } else {
            return uni.showToast({ title: res.data.msg, icon: 'none' })
          }
        },
				fail: err => {
					return reject(err)
				},
        complete: () => {
          if(loadingState) uni.hideLoading()
        }
			})
		)
	},
  put: (url, data = {}, loadingState = false) => {
    if(loadingState) uni.showLoading({ title: '请稍等...' })
  	return new Promise((resolve, reject) =>
  		uni.request({
  			url: REQUEST_URL + url, method: "PUT", data,
        header: { 'Authorization': `Bearer ${getToken()}`, 'tenant-id':  '1', 'content-type': 'application/json; charset=UTF-8' },
        success: res => {
          if (res.data.code === 0) {
            return resolve(res.data.data)
          } else if (res.data.code === 401) {
            setLogout()
            uni.navigateTo({ url: '/pages/login?routeBack=1' })
          } else {
            return uni.showToast({ title: res.data.msg, icon: 'none' })
          }
        },
  			fail: err => {
  				return reject(err)
  			},
        complete: () => {
          if(loadingState) uni.hideLoading()
        }
  		})
  	)
  },
	upload: (url, filePath, data = {}, loadingState = false) => {
    if(loadingState) uni.showLoading({ title: '请稍等...' })
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: REQUEST_URL + url + `?${convertObjToURI(data)}`, name: 'avatar', filePath, fileType: 'image',
        header: { 'Authorization': `Bearer ${getToken()}`, 'tenant-id':  '1', 'content-type': 'application/json; charset=UTF-8' },
				success: resp => {
					const res = Object.assign({}, { data: JSON.parse(resp.data) })

          if (res.data.code === 0) {
            return resolve(res.data.data)
          } else if (res.data.code === 401) {
            setLogout()
            uni.navigateTo({ url: '/pages/login?routeBack=1' })
          } else {
            return uni.showToast({ title: res.data.msg, icon: 'none' })
          }
				},
				fail: err => {
					return reject(err)
				},
        complete: () => {
          if(loadingState) uni.hideLoading()
        }
			})
		})
	}
}

export default Request
